from flask import Flask

from controllers.default_controller import DefaultController
from controllers.error_controller import ErrorController
from controllers.face_feature_controller import FaceFeatureController
from controllers.face_matching_controller import FaceMatchingController
from controllers.speech_recognition_controller import SpeechRecognitionController
from controllers.system_controller import SystemController
from controllers.video_processing_controller import VideoProcessingController
from controllers.image_processing_controller import ImageProcessingController
from utils.logger_config import get_logger_config
from utils.path_utils import PathUtils

# 创建日志记录器，明确指定名称为"controllers"
logger = get_logger_config(name="controllers").get_logger()


def register_controllers(app: Flask):
    """
    注册所有控制器

    Args:
        app: Flask应用实例
    """
    # 确保swagger_docs目录存在
    swagger_docs_dir = PathUtils.join_path(PathUtils.get_project_root(), "swagger_docs")
    PathUtils.ensure_dir_exists(swagger_docs_dir)
    logger.info(f"Swagger文档目录: {swagger_docs_dir}")

    # 注册错误处理控制器
    with app.app_context():
        ErrorController(app)

    # 创建控制器实例
    controllers = []

    # 使用应用上下文创建和注册所有控制器
    with app.app_context():
        # 创建控制器实例
        controllers = [
            DefaultController(__name__),
            FaceFeatureController(__name__),
            FaceMatchingController(__name__),
            SpeechRecognitionController(__name__),
            SystemController(__name__),
            VideoProcessingController(__name__),
            ImageProcessingController(__name__),
        ]

        # 注册所有控制器的蓝图
        for controller in controllers:
            app.register_blueprint(controller.get_blueprint())

    return controllers
